package com.study.servlet;

import com.study.dao.CustomerDao;
import com.study.dao.LogDao;
import com.study.dao.impl.CustomerDaoImpl;
import com.study.dao.impl.LogDaoImpl;
import com.study.model.Customer;
import com.study.model.Log;
import com.study.util.GsonHelper;
import com.study.util.PageHelper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author WS
 */
@WebServlet(name = "queryLog",urlPatterns = "/queryLog")
public class LogQueryServlet extends BaseServlet {
    private LogDao dao=new LogDaoImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 解决json中文乱码
        resp.setContentType("text/json;charset=UTF-8");
        resp.setCharacterEncoding("UTF-8");

        String curpagestr=req.getParameter("curpage").isEmpty()?"1":req.getParameter("curpage");
        String pagesizestr=req.getParameter("pagesize").isEmpty()?"10":req.getParameter("pagesize");
        int curpage=Integer.parseInt(curpagestr);
        int pagesize=Integer.parseInt(pagesizestr);
        PrintWriter out = resp.getWriter();
        PageHelper<Log> logs=dao.queryAllByPagerHelper(curpage,pagesize);
        String json= GsonHelper.toJsonWithTime(logs);
        out.println(json);
        System.out.println(json);
        out.flush();
        out.close();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req,resp);
        doGet(req, resp);
    }
}
